<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('order.order.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef" size="small"></Table>

        <!-- 表单 -->
        <PopupForm />
        <orderDetails />
        <userDetails />
 

    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref,markRaw} from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import goodsList from './goods_list.vue'
import orderPrice from './order_price.vue'
import orderInfo from './order_info.vue'
import orderStatus from './order_status.vue'
import orderBuyer from './order_buyer.vue'
import orderTime from './order_time.vue'
import orderDetails from './order_details.vue'
import userDetails from './user_details.vue'
defineOptions({
    name: 'order/order',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons([])
const dialogVisible = ref(false)

//添加button按钮
optButtons.unshift({
    render: 'tipButton',
    name: 'edit',
    title: '操作',
    text: '操作',
    type: 'primary',
    icon: '',
    class: 'table-row-edit',
    disabledTip: false,
});

optButtons.unshift({
    render: 'tipButton',
    name: 'orderDetails',
    title: '详细',
    text: '详细',
    type: 'primary',
    icon: '',
    class: '',
    disabledTip: false,
    click: function (row: TableRow) {
        baTable.form.operate = 'orderDetails'  //显示组件弹窗
        baTable.form.loading = false
        baTable.form.items!.oid = row[baTable.table.pk!]
    },
});

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/order.Order/'),
    {
        pk: 'oid',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('order.order.oid'), prop: 'oid', align: 'center', width: 70, operator:false, sortable: 'custom' },
            { label: t('order.order.ordernum'), prop: 'ordernum', align: 'center', operator: 'LIKE', sortable: false,show:false},
            { label: t('order.order.user_id'), prop: 'user_id', align: 'center', operator: '=',show:false},
            { label: t('order.order.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss',show:false},
            { label: t('order.order.uname'), prop: 'uname', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', show: false },
            { label: t('order.order.tel'), prop: 'tel', align: 'center', operator: 'LIKE', show: false },
            { label: t('order.order.address'), prop: 'address', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', show: false },
            { label: t('order.order.tradernum'), prop: 'tradernum', align: 'center', operator: 'LIKE', show: false },
            { label: t('order.order.remarks'), prop: 'remarks', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', show: false },
            { label: t('order.order.orderdes'), prop: 'orderdes', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', show: false },
            { label: '订单状态',prop: 'order_status', align: 'center',show:false, operator: '=' ,comSearchRender:'remoteSelect',remote: { pk: 'os', field: 'name', remoteUrl: '/admin/order.Order/getOs' }},

            { label: t('order.order.goods_info'),align:'left', prop: 'cart',operator:false,render: 'customRender', customRender: markRaw(goodsList) },
            { label: t('order.order.order_price'),align:'left', prop: 'order_money', width: 200,operator:false,render: 'customRender', customRender: markRaw(orderPrice) },
            { label: "订单信息",align:'left', prop: 'order_info', width: 350,operator:false,render: 'customRender', customRender: markRaw(orderInfo) },
            { label: "订单状态",align:'center', prop: 'order_s', width: 120,operator:false,render: 'customRender', customRender: markRaw(orderStatus) },
            { label: "买家信息",align:'center', prop: 'user', width: 150,operator:false,render: 'customRender', customRender: markRaw(orderBuyer) },
            { label: "时间",align:'left', prop: 'all_time', operator:false,render: 'customRender', customRender: markRaw(orderTime) },
            { label: t('Operate'), align: 'center', width: 130, render: 'buttons', buttons: optButtons, operator: false},
        ],
        dblClickNotEditColumn: [undefined],
        defaultOrder: { prop: 'oid', order: 'desc' },
    },
    {
        defaultItems: { ordernum: 0, is_pay: 0, order_status: 0, uname: null, tel: 0, address: null, tradernum: 0, deliver_time: null, receive_time: null, complete_time: null, money: 0, remarks: null, orderdes: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})


 


</script>

<style scoped lang="scss"></style>
